﻿using FinallyArchive__Application.CURDBaseService;
using FinallyArchive__Comm;
using FinallyArchive__Dto;
using FinallyArchive__Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FinallyArchive__Application
{
    public class UserService : IUserService
    {
        private readonly IBaseService<User, UserInputDto, UserOutputDto, UserSearchDto, int> _service;
        public UserService(IBaseService<User, UserInputDto, UserOutputDto, UserSearchDto, int> service)
        {
            _service = service;
        }

        /// <summary>
        /// 继承借口,实现接口里面的声明和签名,添加方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int AddUser(UserInputDto entity)
        {
            var md =  MD5Helper.MD5Encrypt32(entity.UserAccess);
            entity.UserPwd = md;
            return _service.AddEntity(entity);

        }

        /// <summary>
        /// 继承接口,实现接口里面的声明和签名,显示全部
        /// </summary>
        /// <returns></returns>
        public IQueryable<UserOutputDto> GetUserAll()
        {
            return _service.GetListAll();
        }

        /// <summary>
        /// 继承接口,实现接口里面的声明和签名,条件查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IQueryable<UserOutputDto> GetUserSearch(string userCode)
        {
            var lst = from Qb in _service.GetListAll()
                      where Qb.UserCode.Equals(userCode)
                      select new UserOutputDto()
                      {
                          UserCode = Qb.UserCode,
                          UserName = Qb.UserName,
                          UserAge = Qb.UserAge,
                          UserSex = Qb.UserSex,
                          UseriPhone = Qb.UseriPhone,
                          UserSate = Qb.UserSate.ToString()
                      };
            return lst;
        }

        /// <summary>
        /// 继承接口,实现接口里面的声明和签名,删除(一般情况下在公司里是不用物理删)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public UserOutputDto DelUser(int id)
        {
            var del = _service.DelTEntity(id);
            return del;
        }


        /// <summary>
        /// 调用接口,实现接口中的查询单一信息,实现修改状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sate"></param>
        /// <returns></returns>
        public Result UpUserSate(int id, SateEnum sate)
        {
            //实例化实体,再调用查询单一信息的接口
            User user = _service.InfoSearch(id);
            //搜到之后已经有搜出来的所有信息了
            //然后进行赋值,把传过来的状态重新赋给实体user里面
            user.UserState = sate;
            //判断是否大于0 
            if (_service.UpTEntity(user) > 0)
            {
                //是的话返回统一返回值成功
                return new Result()
                {
                    Code = ReusltEnum.成功,
                    data = _service.UpTEntity(user),
                    Massage = "修改状态成功"
                };
            }
            //不是的话返回统一返回值失败
            return new Result()
            {
                Code = ReusltEnum.失败,
                data = _service.UpTEntity(user),
                Massage = "修改状态失败"
            };
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <returns></returns>
        public Result LoginUser(LoginUserDto login) 
        {
            var lon = _service.GetListAll().
                Where(s => s.UserAccess.Equals(login.UserAccess) && 
              .Equals(login.UserPwd)).AsQueryable();
            if (lon != null) 
            {
                return new Result()
                {
                    Code = ReusltEnum.成功,
                    Massage = "登录成功"
                };
            }
            return new Result()
            {
                Code = ReusltEnum.失败,
                Massage = "登录失败"
            };
        }
    }
}
